{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 02, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> <font color=\"blue\"> Solutions for </font> Matrices: Tensor Product </h2>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task1\"></a>\n",
    "<h3> Task 1 </h3>\n",
    "\n",
    "Find $ u \\otimes v $ and $ v \\otimes u $ for the given vectors $ u = \\myrvector{-2 \\\\ -1 \\\\ 0 \\\\ 1} $ and $ v = \\myrvector{ 1 \\\\ 2 \\\\ 3 } $."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "u = [-2,-1,0,1]\n",
    "v = [1,2,3]\n",
    "\n",
    "uv = []\n",
    "vu = []\n",
    "\n",
    "\n",
    "for i in range(len(u)): # one element of u is picked\n",
    "    for j in range(len(v)): # now we iteratively select every element of v\n",
    "        uv.append(u[i]*v[j]) # this one element of u is iteratively multiplied with every element of v \n",
    "    \n",
    "print(\"u-tensor-v is\",uv)    \n",
    "\n",
    "for i in range(len(v)): # one element of v is picked\n",
    "    for j in range(len(u)): # now we iteratively select every element of u\n",
    "        vu.append(v[i]*u[j]) # this one element of v is iteratively multiplied with every element of u \n",
    "    \n",
    "print(\"v-tensor-u is\",vu)    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task2\"></a>\n",
    "<h3> Task 2 </h3>\n",
    "\n",
    "Find $ A \\otimes B $ for the given matrices\n",
    "$\n",
    "    A = \\mymatrix{rrr}{-1 & 0 & 1 \\\\ -2 & -1 & 2} ~~\\mbox{and}~~ \n",
    "    B = \\mymatrix{rr}{0 & 2 \\\\ 3 & -1 \\\\ -1 & 1 }.\n",
    "$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = [\n",
    "    [-1,0,1],\n",
    "    [-2,-1,2]\n",
    "]\n",
    "\n",
    "B = [\n",
    "    [0,2],\n",
    "    [3,-1],\n",
    "    [-1,1]\n",
    "]\n",
    "\n",
    "print(\"A =\")\n",
    "for i in range(len(A)):\n",
    "    print(A[i])\n",
    "\n",
    "print() # print a line\n",
    "print(\"B =\")\n",
    "for i in range(len(B)):\n",
    "    print(B[i])\n",
    "\n",
    "# let's define A-tensor-B as a (6x6)-dimensional zero matrix\n",
    "AB = []\n",
    "for i in range(6):\n",
    "    AB.append([])\n",
    "    for j in range(6):\n",
    "        AB[i].append(0)\n",
    "\n",
    "    \n",
    "        \n",
    "# let's find A-tensor-B\n",
    "for i in range(2):\n",
    "    for j in range(3):\n",
    "        # for each A(i,j) we execute the following codes\n",
    "        a = A[i][j]\n",
    "        # we access each element of B\n",
    "        for m in range(3):\n",
    "            for n in range(2):\n",
    "                b = B[m][n]\n",
    "                # now we put (a*b) in the appropriate index of AB\n",
    "                AB[3*i+m][2*j+n] = a * b\n",
    "    \n",
    "    \n",
    "\n",
    "print() # print a line\n",
    "print(\"A-tensor-B =\") \n",
    "print() # print a line\n",
    "for i in range(6):\n",
    "    print(AB[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task3\"></a>\n",
    "<h3> Task 3 </h3>\n",
    "\n",
    "Find $ B \\otimes A $ for the given matrices\n",
    "$\n",
    "    A = \\mymatrix{rrr}{-1 & 0 & 1 \\\\ -2 & -1 & 2} ~~\\mbox{and}~~ \n",
    "    B = \\mymatrix{rr}{0 & 2 \\\\ 3 & -1 \\\\ -1 & 1 }.\n",
    "$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = [\n",
    "    [-1,0,1],\n",
    "    [-2,-1,2]\n",
    "]\n",
    "\n",
    "B = [\n",
    "    [0,2],\n",
    "    [3,-1],\n",
    "    [-1,1]\n",
    "]\n",
    "\n",
    "print() # print a line\n",
    "print(\"B =\")\n",
    "for i in range(len(B)):\n",
    "    print(B[i])\n",
    "    \n",
    "print(\"A =\")\n",
    "for i in range(len(A)):\n",
    "    print(A[i])\n",
    "\n",
    "# let's define B-tensor-A as a (6x6)-dimensional zero matrix\n",
    "BA = []\n",
    "for i in range(6):\n",
    "    BA.append([])\n",
    "    for j in range(6):\n",
    "        BA[i].append(0)\n",
    "        \n",
    "# let's find B-tensor-A\n",
    "for i in range(3):\n",
    "    for j in range(2):\n",
    "        # for each B(i,j) we execute the following codes\n",
    "        b = B[i][j]\n",
    "        # we access each element of A\n",
    "        for m in range(2):\n",
    "            for n in range(3):\n",
    "                a = A[m][n]\n",
    "                # now we put (a*b) in the appropriate index of AB\n",
    "                BA[2*i+m][3*j+n] = b * a\n",
    "    \n",
    "    \n",
    "\n",
    "print() # print a line\n",
    "print(\"B-tensor-A =\") \n",
    "print() # print a line\n",
    "for i in range(6):\n",
    "    print(BA[i])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
